<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/seting' }">
                系统设置</el-breadcrumb-item
            >
            <el-breadcrumb-item>添加配置</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-page-header @back="goBack" content="添加配置"> </el-page-header>
            <el-form
                :model="editForm"
                :rules="editFormRules"
                ref="editFormRef"
                label-width="120px"
                hide-required-asterisk
            >
                <el-form-item label="配置分类" prop="category" align="left">
                    <el-select
                        v-model="editForm.category"
                        placeholder="请选择配置分类"
                    >
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="配置名称" prop="name">
                    <el-input
                        v-model="editForm.name"
                        placeholder="请输入配置名称"
                    >
                    </el-input>
                </el-form-item>

                <el-form-item label="配置标识" prop="tag">
                    <el-input
                        v-model="editForm.tag"
                        placeholder="请输入配置标识"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item
                    label="配置内容类型"
                    prop="value_type"
                    align="left"
                >
                    <el-select
                        v-model="editForm.value_type"
                        placeholder="请选择配置内容类型"
                    >
                        <el-option
                            v-for="item in optionsType"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="配置描述" prop="describe">
                    <el-input
                        v-model="editForm.describe"
                        :autosize="{ minRows: 2, maxRows: 4 }"
                        type="textarea"
                        placeholder="请输入配置描述"
                    />
                </el-form-item>
                <el-form-item label="排序" prop="describe">
                    <el-input
                        v-model="editForm.sort"
                        placeholder="请输入排序"
                    />
                </el-form-item>
                <el-form-item label="状态" align="left">
                    <el-switch
                        v-model="editForm.status"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
                <el-form-item align="left">
                    <el-button type="primary" @click="subEdit">保存</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
export default {
    data () {
        return {
            editForm: {
                category: '',
                name: '',
                tag: '',
                value_type: '',
                describe: '',
                sort: 10,
                status: 1
            },
            options: [
                { value: 'system', label: '系统配置' },
                { value: 'platform', label: '平台配置' },
                { value: 'pay', label: '支付配置' }
            ],
            optionsType: [
                { value: 1, label: '文本' },
                { value: 2, label: '文本域' },
                { value: 3, label: '图片' },
                { value: 4, label: '文件' }
            ],
            // 添加表单验证规则对象 tag value_type
            editFormRules: {
                category: [
                    { required: true, message: '请选择配置分类', trigger: 'change' }
                ],
                name: [
                    { required: true, message: '请输入配置名称', trigger: 'blur' },
                    { min: 2, max: 16, message: '配置名称长度在2~16位之间', trigger: 'blur' }
                ],
                tag: [
                    { required: true, message: '请输入配置名称', trigger: 'blur' },
                    { min: 2, max: 16, message: '配置名称长度在2~16位之间', trigger: 'blur' }
                ],
                value_type: [
                    { required: true, message: '请选择配置分类', trigger: 'change' }
                ],
                describe: [
                    { required: true, message: '请输入配置描述', trigger: 'blur' },
                    { min: 2, message: '配置描述长度必须大于2', trigger: 'blur' }
                ]
            }
        }
    },
    created () {
        this.getRow(this.$route.query.id)
    },
    methods: {
        getRow (id) {
            requestApis.getConfigRow({ id }).then(res => {
                if (res.meta.code === 1) {
                    this.editForm.id = res.data.id
                    this.editForm.category = res.data.category
                    this.editForm.name = res.data.name
                    this.editForm.tag = res.data.tag
                    this.editForm.value_type = res.data.value_type
                    this.editForm.describe = res.data.describe
                    this.editForm.sort = res.data.sort
                    this.editForm.status = res.data.status
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                    this.$router.back()
                }
            }).catch(error => {
                console.log(error)
            })
        },
        goBack () {
            this.$router.back()
        },
        subEdit () {
            this.$refs.editFormRef.validate(valid => {
                if (!valid) {
                    return false
                }
                requestApis.saveConfigEdit(this.editForm).then(res => {
                    if (res.meta.code === 1) {
                        this.$message({
                            message: res.meta.msg,
                            type: 'success'
                        })
                        this.$router.back()
                    } else {
                        this.$message({
                            message: res.meta.msg,
                            type: 'warning'
                        })
                    }
                }).catch(error => {
                    console.log(error)
                })
            })
        },
        resetForm () {
            this.$refs.editFormRef.resetFields()
            this.editForm.status = 1
        }
    }
}
</script>

<style>
.el-form {
    width: 60%;
    margin-top: 20px;
}
</style>
